<template>
  <div class="create">
    <h2 class="margin-bom title">{{title}}</h2>

    <div class="userFrom">
      <!-- 表单校验 -->
      <el-form :model="user" :rules="rules" ref="user" status-icon>
        <el-form-item label="账号" prop="name">
          <el-input type="text" v-model="user.name"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="pass">
          <el-input type="password" v-model="user.pass" :disabled="id != ''"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="checkPass" v-if="id == ''">
          <el-input type="password" v-model="user.checkPass"></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="tel">
          <el-input type="text" v-model="user.tel"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input type="text" v-model="user.email"></el-input>
        </el-form-item>
        <el-form-item label="性别">
          <el-radio v-model="user.sex" label="男" :disabled="id != ''">男</el-radio>
          <el-radio v-model="user.sex" label="女" :disabled="id != ''">女</el-radio>
        </el-form-item>
        <el-form-item label="身份证号" prop="idx">
          <el-input type="text" v-model="user.idx" :disabled="id != ''"></el-input>
        </el-form-item>
        <el-form-item label="备注">
          <el-input type="text" v-model="user.des"></el-input>
        </el-form-item>
        <el-form-item label="生日" prop="time">
          <el-date-picker v-model="user.time" value-format="yyyy-MM-dd"  type="date" placeholder="选择日期"></el-date-picker>
        </el-form-item>

        <el-form-item>
          <div class="text-center">
            <el-button type="primary" @click="createUser()" v-if="id == ''">添加</el-button>
            <el-button type="success" @click="updateUser()" v-if="id != ''">修改</el-button>
            <el-button @click="back()">返回</el-button>
          </div>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
import ajax from "../../common/js/ajax";
import API from "../../common/js/api";
import rles from "../../common/js/userRules";
export default {
  data() {
    return {
      title: "",
      id: "",
      user: {
        name: "",
        pass: "",
        checkPass: "",
        tel: "",
        email: "",
        sex: "男",
        idx: "",
        tes: "",
        time: ""
      },
      // 校验规则
      rules: rles(this)
    };
  },
  methods: {
    checkSubmit(url) {
      this.$refs.user.validate(valid => {
        if (url == "/api/addUser") {
          // 是添加
          delete this.user.checkPass;
        } else if (url == "/api/updateUser") {
          delete this.user._id;
          this.user.id = this.id;
        }
        if (valid) {
          ajax(
            url,
            "get",
            this.user,
            d => {
              this.$message({
                message: `${this.title}成功`,
                type: "success"
              });
              this.$router.replace("/index/userList");
            },
            err => {
              this.$message({
                message: err.data.info,
                type: "warning"
              });
            }
          );
        }else{
          this.$message({
            message:'请填写字段',
            type:"warning"
          })
        }
      });
    },
    createUser() {
      // 新建用户
      // 校验表单
      this.checkSubmit(API.addUser);
    },
    updateUser() {
      // 修改用户
      this.checkSubmit(API.updateUser);
    },
    back() {
      this.$router.go(-1);
    }
  },
  watch: {},
  computed: {},
  mounted() {

    let len = Object.keys(this.$route.query);
    if (len.length == 0) {
      this.title = "添加用户";
    } else {
      this.title = "修改用户";
      this.id = this.$route.query.id;
      ajax(
        API.findUser,
        "get",
        { id: this.id },
        d => {
          this.user = d.data.data[0];
        },
        err => {
          alert(err.data.info);
        }
      );
    }
  }
};
</script>
<style lang="stylus" scoped>
@import '../../common/css/index.styl';

.el-col {
  line-height: 40px;
}

.create >>> .el-row {
  min-width: 700px;
}

.create >>> .el-input {
  float: right;
  width: 89%;
}

.create >>> .el-form-item__error {
  left: 100px;
}

.create >>> .el-form-item__content {
  text-align: left;
}

.create >>> .el-radio:last-child {
  margin-left: 0;
}

.create >>> .el-radio {
  margin-left: 50px;
}

.el-date-editor--date {
  margin-left: 40px !important;
  float: left !important;
  width: 30% !important;
}

.userFrom {
  width: 60%;
  margin: 0 auto;
  min-width: 700px;
}
</style>